// pages/quarter/quarter.js
Page({

    data: {
        date: '',
        quarterArray: [
            ['2018', '2019', '2020', '2021', '2022', '2023'],
            ['第一季度', '第二季度', '第三季度', '第四季度']
        ],
        quarterIndex: [0, 0]
    },

    onLoad(options) {
        setTimeout(() => {
            this.getDate()
        }, 100)
    },

    //   获取当前日期
    getDate() {
        const date = new Date()
        // 转换为YYYY-MM格式
        const year = date.getFullYear()
        // 生成前10年
        const yearArray = []
        for (let i = 0; i < 10; i++) {
            yearArray.unshift(year - i)
        }
        yearArray.push(year + 1)
        // 获取当前季度，生成quarterIndex
        const month = date.getMonth() + 1
        let quarterIndex = 0
        if (month >= 1 && month <= 3) {
            quarterIndex = 0
        }
        if (month >= 4 && month <= 6) {
            quarterIndex = 1
        }
        if (month >= 7 && month <= 9) {
            quarterIndex = 2
        }
        if (month >= 10 && month <= 12) {
            quarterIndex = 3
        }

        this.setData({
            date: year,
            'quarterArray[0]': yearArray,
            'quarterIndex[0]': yearArray.indexOf(year),
            'quarterIndex[1]': quarterIndex,
            date: year + '-' + '0' + ((quarterIndex + 1) * 3 - 2)
        })
    },

    //   选择日期
    bindQuarterChange(e) {
        let quarter = null
        if (e.detail.value[1] == 0) {
            quarter = '01'
        }
        if (e.detail.value[1] == 1) {
            quarter = '04'
        }
        if (e.detail.value[1] == 2) {
            quarter = '07'
        }
        if (e.detail.value[1] == 3) {
            quarter = '10'
        }
        // console.log(this.data.quarterArray[0][e.detail.value[0]] + quarter)
        this.setData({
            date: this.data.quarterArray[0][e.detail.value[0]] + '-' + quarter,
            quarterIndex: e.detail.value
        })
    }
})